#!/usr/bin/env python3
import pymysql
from config import *
def getMySql(mysqlHost,mysqlUser,mysqlPW,mysqlDB):
    db = pymysql.connect(mysqlHost,mysqlUser,mysqlPW,mysqlDB)
    cursor = db.cursor()
    sql = """select * from  vertex"""
    try:
        cursor.execute(sql)
        result = cursor.fetchall()
    except:
        print("select error")
    db.close()
    return result

def createOriginGraph(result):
    for i in range(len(result)):
        childMap = []
        relation = [ int(m) for m in result[i][3].split(',')]
        distance = [int(n) for n in result[i][5].split(',')]
        for j in range(len(result)):
            if j == i:
                childMap.append(0)
                continue
            if j in relation:
                index = relation.index(j)
                childMap.append(distance[index])
                continue
            childMap.append(gigantic)
        OriginGraph.append(childMap)

def getvertex(mysqlHost,mysqlUser,mysqlPW,mysqlDB,path):
    PATH = {}
    index = 0
    db = pymysql.connect(mysqlHost,mysqlUser,mysqlPW,mysqlDB)
    cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
    sql = """ select nodeID,cast(lon as char) as lon,cast(lat as char) as lat from vertex where nodeID=%s"""
    for i in path:
        for j in i:
            try:
                cursor.execute(sql%(j))
                result = cursor.fetchone()
                PATH[str(index)] = result
                index += 1
            except:
                print("select lon error")
    db.close()
    return PATH
    
#result = getMySql()
#createOriginGraph(result)
#print(OriginGraph)
#getvertex(mysqlHost,mysqlUser,mysqlPW,mysqlDB,path)
